package com.cheyun.netsalev3.view

import android.widget.LinearLayout
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.ActivityAnalysisBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.just.agentweb.AgentWeb
import android.text.TextUtils
import android.webkit.WebView
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.just.agentweb.WebChromeClient
import java.net.URLEncoder
import android.graphics.Bitmap
import android.view.KeyEvent
import android.view.View
import android.widget.TextView
import com.just.agentweb.WebViewClient


class H5Activity : BaseActivity<ActivityAnalysisBinding>() {
    override fun initView() {

        binding.leftBut.setOnClickListener {
            back()
        }
    }

    override fun initContentView(): Int {
        return R.layout.activity_analysis
    }

    var mAgentWeb:AgentWeb? = null
    override fun initData() {

        var url = intent.getStringExtra("url")

        mAgentWeb = AgentWeb.with(this)//传入Activity
                .setAgentWebParent(binding.llWebview, LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go(url)

        mAgentWeb?.webCreator?.webView?.setWebChromeClient(object : WebChromeClient() {
            override fun onReceivedTitle(view: WebView, title: String) {
                if (!TextUtils.isEmpty(title)) {
                    binding.titleText.text = title
                }
                super.onReceivedTitle(view, title)
            }

            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                DemoLog.i("AnalysisActivity", "newProgress:" + newProgress)
                mAgentWeb?.indicatorController?.setProgress(newProgress)
            }
        })
    }

    private val mWebViewClient = object : WebViewClient() {
        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
            DemoLog.i("AnalysisActivity", "url:" + url)
        }
    }

    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mAgentWeb?.webLifeCycle?.onDestroy()
        super.onDestroy()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            back()
        }
        return true
    }
    fun back(){
        if(!mAgentWeb?.back()!!){
            finish()
        }
    }


}
